//
//  UITabBar.m
//  HCHK_iphone
//
//  Created by yilong zhang on 12-3-21.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UITabBarEx.h"

//================================================================================================================

@implementation CTabBarItem

@synthesize delegate;
@synthesize m_NormalPath;
@synthesize m_SelectedPath;
@synthesize m_Text;
@synthesize m_BackGroundImage;
@synthesize m_IsSelected;

-(id)initWithImageSourcePath:(NSString *)normalpath SelectedPath:(NSString *)selectedpath Text:(NSString*)text
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.autoresizesSubviews = YES;
        
        self.m_NormalPath   = normalpath;
        //self.m_SelectedPath = selectedpath;
        
        m_BackGroundImage = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage imageNamed:m_NormalPath];
        m_BackGroundImage.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        m_BackGroundImage.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
        m_BackGroundImage.backgroundColor = [UIColor clearColor];
        [m_BackGroundImage setBackgroundImage:image forState:UIControlStateNormal];
        [m_BackGroundImage addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        //m_BackGroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self addSubview:m_BackGroundImage];
        //[m_BackGroundImage release];
        
        /*m_Text = [[UILabel alloc] init];
        m_Text.text = text;
        m_Text.textColor = [UIColor colorWithRed:0.964 green:0.968 blue:0.949 alpha:1.0];
        m_Text.backgroundColor = [UIColor clearColor];
        m_Text.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        m_Text.textAlignment = UITextAlignmentCenter;
        m_Text.font = [UIFont fontWithName:@"Helvetica" size:11];
        [self addSubview:m_Text];
        [m_Text release];*/
        
        m_IsSelected = NO;
    }
    
    return self;
}

-(void)dealloc
{
    [m_NormalPath release];
    [m_SelectedPath release];
    
    [super dealloc];
}

-(void)SetValue:(BOOL)value
{
    m_IsSelected = value;
    
    if (value)
    {
        //m_BackGroundImage.image = [UIImage imageNamed:m_SelectedPath];
        [m_BackGroundImage setHighlighted:YES];
    }
    else 
    {
        //m_BackGroundImage.image = [UIImage imageNamed:m_NormalPath];
        [m_BackGroundImage setHighlighted:NO];
    }
}

-(void)LayoutSubviews
{
    //m_Text.frame = CGRectMake(0, self.frame.size.height-12, self.frame.size.width, 12);
    //m_BackGroundImage.frame = CGRectMake((self.frame.size.width-25)/2, 5, 25, 25);
    m_BackGroundImage.frame = CGRectMake(0, 0, m_BackGroundImage.frame.size.width, m_BackGroundImage.frame.size.height);
    m_BackGroundImage.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+5);
}

-(void)OnButtonDown:(UIButton*)sender
{
    [delegate DidTabBarItemSelected:self];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self SetValue:YES];
    
    [delegate DidTabBarItemSelected:self];
}

@end

//================================================================================================================

@implementation UITabBarEx

@synthesize delegate;
@synthesize m_Items;
@synthesize m_Cursor;
@synthesize m_nOldCursor;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, -2, frame.size.width, frame.size.height+2)];
        back.image = [UIImage imageNamed:@"tabbargb.png"];
        [self addSubview:back];
        [back release];
        
        //m_Cursor = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbarcursor.png"]];
        m_Cursor = [[UIImageView alloc] init];
        m_Cursor.tag = 0;
        m_Cursor.alpha = 0.0;
        [self addSubview:m_Cursor];
        [m_Cursor release];
    }
    return self;
}

-(void)dealloc
{
    [m_Items release];
    
    [super dealloc];
}

-(void)SetItems:(NSArray *)items
{
    int nCnt = [self.m_Items count];
    self.m_Items = items;
    float fwidth = self.frame.size.width / [items count];
    m_Cursor.frame = CGRectMake(-fwidth, 0, fwidth, self.frame.size.height);

    for (CTabBarItem *item in items)
    {
        item.frame = CGRectMake(nCnt*fwidth, 0, fwidth, self.frame.size.height);
        item.tag = nCnt;
        [item LayoutSubviews];
        item.delegate = self;
        [self addSubview:item];
        [item release];
        
        ++nCnt;
    }
}

-(int)GetCursor
{
    return m_Cursor.tag;
}

-(int)GetProCursor
{
    return m_nOldCursor;
}

-(void)SetItemHight:(CTabBarItem*)item IsHight:(BOOL)ishight
{
    [item SetValue:ishight];
}

-(void)SetSelectedItem:(CTabBarItem *)item
{
    m_nOldCursor = m_Cursor.tag;
    [[self.m_Items objectAtIndex:m_Cursor.tag] SetValue:NO];
    
    m_Cursor.alpha = 1.0;
    m_Cursor.center = CGPointMake(item.center.x, item.center.y);
    [item SetValue:YES];
    m_Cursor.tag = item.tag;
}

-(void)DidTabBarItemSelected:(CTabBarItem *)item
{
    m_nOldCursor = m_Cursor.tag;
    [[self.m_Items objectAtIndex:m_Cursor.tag] SetValue:NO];
    
    m_Cursor.center = CGPointMake(item.center.x, item.center.y);
    m_Cursor.alpha = 1.0;
    m_Cursor.tag = item.tag;
    
    [delegate TabBarEx:self TabBarItem:item];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
